package com.neo.scalaCookBook.C1_String

/**
  * Autuor : zhaoshun
  * Time   : 2017/10/17 0:28
  * Email  : tmby1314@163.com
  **/


/**
  * 比较两个字符串
  */
object StringCompare {
  def main(args: Array[String]): Unit = {
    val s0 = "Hello world!"
    //获取字符串长度
    println(s0.length)

    println("连接字符创")
    val s1 = "Hello" + " world"
    println(s1)
    println("把字符型串当作字符序列来处理1")
    "hello".foreach(println)
    println("把字符型串当作字符序列来处理2")
    for (c <- "Hello") println(c)


    println("高级for推导式")
    advanceFor()
    println("把字符型串当作字节序列来处理2")
    s1.getBytes.foreach(println)

    println("过滤字符串序列")
    val result = "Hello world".filter(_ != 'l')
    println(result)



    println("像封闭类添加方法")
    val s2 ="scala".drop(2)
    println(s2)
    println("新的接口已经用切片'.slice'来替代了")
    val s3 ="scala".drop(2).take(2).capitalize

    println(s3)

    println("字符串比较")
    val s4 = "Hello"
    val s5 = "Hello"
    val s6 = "H" + "ello"
    val s7 = null
    println("利用‘==’直接比较")
    println(s4 == s5)
    println("无需做'null'值判断，不会抛出NullPointerException")
    println(s4 == s7)
    println("忽略大小写，可以先转换")
    println(s4.toUpperCase == s5.toUpperCase())
    println("但是在'null'上是不允许的")
    //println(s4.toLowerCase == s7.toLowerCase)
    println("也可用JAVA中的方法来比较")
    println(s4.equalsIgnoreCase(s5))

  }


  def advanceFor(): Unit ={
    for(i <- 1 to 10 if i <5 ; j <- 1 to 20 if j < i ) println(i + " " + j)
  }
}


